package com.erytech.linktalk.web.client.dataview;

import com.erytech.linktalk.web.client.datasource.PortraintChangeListener;
import com.erytech.linktalk.web.client.datasource.PortraitDataSource;
import com.erytech.linktalk.web.client.datasource.RoomDataSource;
import com.erytech.linktalk.web.client.datasource.UserDataSource;
import com.erytech.linktalk.web.client.datasource.UserUpdateEvent;
import com.erytech.linktalk.web.client.framework.DataUpdateEvent;
import com.erytech.linktalk.web.client.framework.DataView;
import com.erytech.linktalk.web.client.i18n.LocaleMessages;
import com.erytech.linktalk.web.client.service.UserInfo;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginView extends DataView implements ClickListener {

	private TextBox nameBox;
	
	private ListBox genderList;
	
	private TextArea descrArea;

	private Button loginButton;

	private Image portraitImage;
	
	private Image waitImage;
	
	public LoginView(RoomDataSource roomDS) {
		super(roomDS.getUserDataSource());
		getPortraitDataSource().addDataUpdateListener(this);
		initWidget(createDockPanel());
	}

	private Widget createDockPanel() {
		DockPanel outer = new DockPanel();
		outer.setSpacing(8);
		outer.add(ImageFactory.getLogo(), DockPanel.NORTH);
		outer.add(createLoginPanel(), DockPanel.CENTER);
		outer.add(createButtonPanel(), DockPanel.SOUTH);
		return outer;
	}

	private Widget createButtonPanel() {
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		buttonPanel.add(getLoginButton());
		buttonPanel.add(getWaitImage());
		return buttonPanel;
	}

	private Widget createLoginPanel() {
		VerticalPanel loginPanel = new VerticalPanel();
		loginPanel.setSpacing(8);
		loginPanel.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
		loginPanel.add(createLabel(LocaleMessages.instance.name()));
		loginPanel.add(getNameBox());
		loginPanel.add(createLabel(LocaleMessages.instance.gender()));
		loginPanel.add(getGenderList());
		loginPanel.add(createLabel(LocaleMessages.instance.portrait()));
		loginPanel.add(getPortraitImage());
		loginPanel.add(createLabel(LocaleMessages.instance.introduction()));
		loginPanel.add(getDescrArea());
		return loginPanel;
	}

	private Button getLoginButton() {
		if (loginButton == null) {
			loginButton = new Button(LocaleMessages.instance.login());
			loginButton.addClickListener(this);
		}
		return loginButton;
	}
	
	private Image getWaitImage() {
		if(waitImage == null) {
			waitImage = ImageFactory.getWaitImage();
			waitImage.setVisible(false);
		}
		return waitImage;
	}
	
	private Image getPortraitImage() {
		if(portraitImage == null) {
			portraitImage = new Image(getPortraitDataSource().getSelectedPortrait().getUrl());
			portraitImage.setStyleName("portrait-image-outset");
			portraitImage.addClickListener(new ClickListener() {
				public void onClick(Widget widget) {
					portraitImage.setStyleName("portrait-image-inset");
					PortraitView portraitView = new PortraitView(getPortraitDataSource());
					portraitView.setPopupPosition(portraitImage.getAbsoluteLeft() + 40, portraitImage.getAbsoluteTop() - 120);
					portraitView.show();
				}
			});
		}
		return portraitImage;
	}

	private PortraitDataSource getPortraitDataSource() {
		PortraitDataSource portraitDataSource = ((UserDataSource)getDataSource())
			.getRoomDataSource().getPortraitDataSource();
		return portraitDataSource;
	}

	private ListBox getGenderList() {
		if (genderList == null) {
			genderList = new ListBox();
			genderList.setWidth("300px");
			genderList.addItem(LocaleMessages.instance.male());
			genderList.addItem(LocaleMessages.instance.female());
			genderList.setVisibleItemCount(1);
		}
		return genderList;
	}

	private TextArea getDescrArea() {
		if (descrArea == null) {
			descrArea = new TextArea();
			descrArea.setWidth("300px");
			descrArea.setHeight("40px");
		}
		return descrArea;
	}

	private TextBox getNameBox() {
		if (nameBox == null) {
			nameBox = new TextBox();
			nameBox.setWidth("300px");
		}
		return nameBox;
	}

	public void onClick(Widget widget) {
		String name = getNameBox().getText();
		if(name == null || name.length() == 0) {
			Window.alert(LocaleMessages.instance.errorName());
			getNameBox().setFocus(true);
			return;
		}
		
		getLoginButton().setEnabled(false);
		getWaitImage().setVisible(true);
		
		UserInfo userInfo = new UserInfo(name.trim(), getDescrArea().getText(), 
				getGenderList().getSelectedIndex() + 1);
		userInfo.setImage(getPortraitDataSource().getSelected());
		((UserDataSource)getDataSource()).login(userInfo);
	}

	private Widget createLabel(String text) {
		Label label = new Label(text + ": ");
		label.setWidth("100%");
		label.setHorizontalAlignment(Label.ALIGN_LEFT);
		label.setStyleName("name-Label");
		return label;
	}

	public void onDataUpdateEvent(DataUpdateEvent event) {
		if(event instanceof UserUpdateEvent) {
			UserUpdateEvent loginEvent = (UserUpdateEvent)event;
			if(loginEvent.getType() == UserUpdateEvent.LOGIN_SUCCESS) {
				getLoginButton().setEnabled(true);
				getWaitImage().setVisible(false);
			} else if(loginEvent.getType() == UserUpdateEvent.LOGIN_FAILURE) {
				getLoginButton().setEnabled(true);
				getWaitImage().setVisible(false);
				Window.alert(loginEvent.getThrowable().getMessage());
			} else if(loginEvent.getType() == UserUpdateEvent.LOGOUT_SUCCESS) {
				//nothing to do
			} else if(loginEvent.getType() == UserUpdateEvent.LOGOUT_FAILURE) {
				//nothing to do
			}
		} else if(event instanceof PortraintChangeListener) {
			PortraitDataSource portraitDataSource = (PortraitDataSource)event.getSrc();
			portraitImage.setUrl(portraitDataSource.getPortraitSrc(portraitDataSource.getSelected()));
			portraitImage.setStyleName("portrait-image-outset");
		}
	}

}
